package com.eco.fanliapp.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.SyUserMoneyLogMapper;
import com.eco.fanliapp.entity.SyUserMoneyLog;
import com.eco.fanliapp.service.ISyUserMoneyLogService;
import com.eco.fanliapp.vo.SyUserMoneyLogVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账户余额日志 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-10-21
 */
@Service
public class SyUserMoneyLogServiceImpl extends ServiceImpl<SyUserMoneyLogMapper, SyUserMoneyLog> implements ISyUserMoneyLogService {


    private static final Logger log = LoggerFactory.getLogger(SyUserMoneyLogServiceImpl.class);

    @Autowired
    private SyUserMoneyLogMapper syUserMoneyLogMapper;

    /**
     * 添加账户变更记录
     * @param userid  用户id
     * @param Balance  账户变更前余额
     * @param amount  变动金额
     * @param remark  变动原因
     * @param logType  日志类型
     * @param logStatus  状态记录
     */
    @Override
    public void addSyUserMoneyLog(Long userid, BigDecimal Balance, BigDecimal amount, String remark, Integer logType, Integer logStatus) {
        //amount 提现金额 ,logType 变动余额类型
        //变动前可用余额
        BigDecimal beforeBalance = Balance;
        log.info("用户userid:{}，账户变动前余额:{}", userid, beforeBalance);
        //提现金额
        BigDecimal amountDecimal = amount;
        log.info("用户userid:{}，提现金额:{}", userid, amount);
        //变动后可用余额
        BigDecimal afterBalance = new BigDecimal("0");

        try {
            if (logStatus == 0) {
                afterBalance = beforeBalance.add(amountDecimal);
            }
            if (logStatus == 1) {
                afterBalance = beforeBalance.subtract(amountDecimal);
            }
            log.info("用户userid:{}，变动后可用余额:{},处理状态:{}", userid, afterBalance, logStatus);

            SyUserMoneyLog syUserMoneyLog = new SyUserMoneyLog();
            syUserMoneyLog.setUserId(userid);
            syUserMoneyLog.setBeforeBalance(beforeBalance);
            syUserMoneyLog.setAmount(amountDecimal);
            syUserMoneyLog.setAfterBalance(afterBalance);
            syUserMoneyLog.setRemark(remark);
            syUserMoneyLog.setLogStatus(logStatus);
            syUserMoneyLog.setLogType(logType);
            int i = syUserMoneyLogMapper.insert(syUserMoneyLog);
            if (i > 0) {
                log.info("账户变更日志记录成功");
            } else {
                log.info("账户变更日志记录失败");
            }
        } catch (Exception e) {
            log.info("账户变更日志记录异常");
        }

    }

    @Override
    public ServerResponse selectUserMoneyLogs(Integer currentPage, Integer pageSize, String searchKey, Integer logType) {
        PageHelper.startPage(currentPage, pageSize);

        Map<String,Object> map = new HashMap<>();
        map.put("searchKey",searchKey);
        map.put("logType",logType);
        List<SyUserMoneyLogVo> list = syUserMoneyLogMapper.selectUserMoneyLogs(map);
        return ServerResponse.createBySuccess("查询成功",new PageInfo(list));
    }

    @Override
    public void addSyUserMoneyLogNew(Long userid, BigDecimal Balance, BigDecimal amount, String remark, Integer logType, Integer logStatus,BigDecimal afterBalance) {
        //amount 提现金额 ,logType 变动余额类型
        //变动前可用余额
        log.info("用户userid:{}，账户变动前余额:{}", userid, Balance);
        try {
            SyUserMoneyLog syUserMoneyLog = new SyUserMoneyLog();
            syUserMoneyLog.setUserId(userid);
            syUserMoneyLog.setBeforeBalance(Balance);
            syUserMoneyLog.setAmount(amount);
            syUserMoneyLog.setAfterBalance(afterBalance);
            syUserMoneyLog.setRemark(remark);
            syUserMoneyLog.setLogStatus(logStatus);
            syUserMoneyLog.setLogType(logType);
            int i = syUserMoneyLogMapper.insert(syUserMoneyLog);
            if (i > 0) {
                log.info("账户变更日志记录成功");
            } else {
                log.info("账户变更日志记录失败");
            }
        } catch (Exception e) {
            log.info("账户变更日志记录异常");
        }
    }

    @Override
    public void addSyUserMoneyLogNew2(Long userid, BigDecimal Balance, BigDecimal amount, String remark, Integer logType) {
        //amount 提现金额 ,logType 变动余额类型
        //变动前可用余额
        log.info("用户userid:{}，账户变动前余额:{}", userid, Balance);
        try {
            SyUserMoneyLog syUserMoneyLog = new SyUserMoneyLog();
            syUserMoneyLog.setUserId(userid);
            syUserMoneyLog.setBeforeBalance(Balance);
            syUserMoneyLog.setAmount(amount);
            syUserMoneyLog.setAfterBalance(amount.add(Balance));
            syUserMoneyLog.setRemark(remark);
            syUserMoneyLog.setLogStatus(1);
            syUserMoneyLog.setLogType(logType);
            int i = syUserMoneyLogMapper.insert(syUserMoneyLog);
            if (i > 0) {
                log.info("账户变更日志记录成功");
            } else {
                log.info("账户变更日志记录失败");
            }
        } catch (Exception e) {
            log.info("账户变更日志记录异常");
        }
    }

}
